var dataSource = new Array();
//table的数据源
var table;   //table对象
dataSource = [];
function accountList(data) {
    dataSource.splice(0,dataSource.length);//清空数组
    $.each(data.userList, function (i, field) {
        var rowData = new Array();
        rowData.push(field.NUMBER);
        rowData.push(field.ROLE_NAMES);
        rowData.push(field.DEPARTMENT_NAME);
        rowData.push(field.PHONE);
        rowData.push(field.EMAIL);
        rowData.push(field.CREAT_TIME);
        rowData.push(field.STATUS);
        rowData.push(field.NAME);
        rowData.push(field.USERNAME);
        rowData.push(field.BIANMA);
        rowData.push(field.USER_ID);
        rowData.push(field.ROLE_ID);
        rowData.push(field.PASSWORD);
        rowData.push(field.ENTRY_TIME);
        rowData.push(field.ROLE_IDS);
        dataSource.push(rowData);
    });
    initDataTable(dataSource);
    function initDataTable(dataSource) {
        var dttable = $('#datatable').dataTable( ); // 初始化dataTable，清空上一个值
        dttable.fnClearTable(); //清空一下table
        dttable.fnDestroy(); //还原初始化了的datatable
        $("#datatable").DataTable({
            "fnDrawCallback" : function(){
                    //权限控制函数
                    power()
            },
            "autoWidth": false,
            "ordering": true,
            "searching": true,
            "paging": true,
            "retrieve": true,
            "destroy": true,
            "bAutoWidth": true,
            "bLengthChange": true,
            "bSort": true,
           "aaSorting": [[5,'desc'],],//数据排序
            "aLengthMenu":[10,20,30],
            "oLanguage": {
                "sLengthMenu": "每页显示 _MENU_ 条记录",
                "sZeroRecords": "抱歉， 没有找到",
                "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
                "sInfoEmpty": "没有数据",
                "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
                "oPaginate": {
                    "sFirst": "首页",
                    "sPrevious": "前一页",
                    "sNext": "后一页",
                    "sLast": "尾页"
                },
                "sZeroRecords": "没有检索到数据",
                "sProcessing": "<img src='./loading.gif' />"
            },
            data: dataSource,
            columnDefs: [
                {
                    "visible": true,
                    "targets": 0
                },
                {
                    "render": function(data,type,row,meta){
                        var val = '<span>工号：'+row[0]+'</span><br><span>姓名：'+row[7]+'</span><br><span>用户名：'+row[8]+'</span>';
                        return val;
                    },
                    "targets": 0
                },
                {
                    "render": function(data,type,row,meta){
                        if(data == null){
                            var val ="";
                            return val;
                        }else {
                            var val = row[1];
                            return val;
                        }
                    },
                    "targets": 1
                },
                {
                    "render": function(data,type,row,meta){
                        if(data == null){
                            var val = "";
                            return val;
                        }else {
                            var val = '<span class="dropDown"><a class="a_remove"><div title="'+row[9]+'">'+row[2]+'</div></a></span>';
                            return val;
                        }

                    },
                    "targets": 2
                },
                {
                    "render": function(data,type,row,meta){
                        if(data == null){
                            var val ="";
                            return val;
                        }else {
                            var val = row[3];
                            return val;
                        }
                    },
                    "targets": 3
                },
                {
                    "render": function(data,type,row,meta){
                        if(data == null){
                            var val ="";
                            return val;
                        }else {
                            var val = row[4];
                            return val;
                        }
                    },
                    "targets": 4
                },
                {
                    "render": function(data,type,row,meta){
                        if(data == null){
                            var val ="";
                            return val;
                        }else {
                             var val = '<span>'+row[5]+'</span><br>';
                             return val;
                        }
                    },
                    "targets": 5
                },
                {
                    "render": function(data,type,row,meta) {
                        if (data == null) {
                            var val = "";
                            return val;
                        } else {
                            var val = '<span>' + row[13] + '</span><br>';
                            return val;
                        }
                    },
                    "targets": 6
                },
                {
                    "render": function(data,type,row,meta){
                        if(row[6]=="0"){
                            /*var val = '<span class="dropDown"> <a class="dropDown_A" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">开启<i class="Hui-iconfont">&#xe6df;</i></a><ul class="dropDown-menu menu radius box-shadow"><li onclick="user_stop(\'\',\''+ row[10] +'\')"><a href="#">禁用</a></li></ul></span>';*/
                            var val = '<span class="dropDown"> <a class="dropDown_A" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">开启<i class="Hui-iconfont">&#xe6df;</i></a><ul class="dropDown-menu menu radius box-shadow"><li><a href="#">禁用</a></li></ul></span>';
                            return val;
                        }else{
                            /*var val = '<span class="dropDown"> <a class="dropDown_A" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">禁用<i class="Hui-iconfont">&#xe6df;</i></a><ul class="dropDown-menu menu radius box-shadow"><li onclick="user_start(\'\',\''+ row[10] +'\')"><a href="#">开启</a></li></ul></span>';*/
                            var val = '<span class="dropDown"> <a class="dropDown_A" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">禁用<i class="Hui-iconfont">&#xe6df;</i></a><ul class="dropDown-menu menu radius box-shadow"><li><a href="#">开启</a></li></ul></span>';
                            return val;
                        }
                    },
                    "targets": 7
                },
                {
                    "render": function(data,type,row,meta){
                        var val = '<a><span class="list powerButton 1000001004725699" onclick="ood_edit(\'修改信息\',\'account-add.html\',\''+row+'\')">编辑</span></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a><span class="list powerButton 1000001004725699" onclick="configuration(\'数据配置\',\'data-configuration.html\',\''+row[10]+'\')">数据配置</span></a>';
                        return val;
                    },
                    "targets": 8
                }
            ]
        });
    };
    power();
}

//    添加
function user_add(title,url){
    /*定义添加用户页默认角色员工*/
    $(function () {
        var roleIdArr = [2];
        var roleNameArr = ["员工"];
        sessionStorage.setItem("roleIdArr", JSON.stringify(roleIdArr));
        sessionStorage.setItem("roleNameArr", JSON.stringify(roleNameArr));
    });
    var editData = {
        USER_ISEIDT:false
    };
    window.session_storage.setItem('user_editData',JSON.stringify(editData));
    var index = layer.open({
        type: 2,
        title: title,
        content: url
    });
    layer.full(index);
}
/*账户信息编辑*/
function ood_edit(title,url,row) {
    console.log(row);
    var anyue= row.split(",");
    //更改页面缓存
    var user_editData = {
        NUMBER:anyue[0],
        ROLE_NAME:anyue[1],
        DEPARTMENT_NAME:anyue[2],
        PHONE:anyue[3],
        EMAIL:anyue[4],
        CREAT_TIME:anyue[5],
        STATE:anyue[6],
        NAME:anyue[7],
        USERNAME:anyue[8],
        BIANMA:anyue[9],
        USER_ID:anyue[10],
        ROLE_ID:anyue[11],
        PASSWORD:anyue[12],
        ENTRY_TIME:anyue[13],
        ROLE_IDS:anyue[14],
        USER_ISEIDT:true
    };
    window.session_storage.setItem('user_editData',JSON.stringify(user_editData));
    var roleIdArr =[];

    var roleIdS = user_editData.ROLE_IDS.split(" ");
    if( roleIdS ==""){
        roleIdS = [];
        roleIdS.push(2);
    }
    for (var i = 0; i < roleIdS.length; i++) {
        roleIdArr.push(parseFloat(roleIdS[i]));
    }
    var roleNameArr =user_editData.ROLE_NAME.split(" ");
    if(roleNameArr == ""){
        roleNameArr = [];
        roleNameArr.push("员工")
    }
    sessionStorage.setItem("roleIdArr", JSON.stringify(roleIdArr));
    sessionStorage.setItem("roleNameArr", JSON.stringify(roleNameArr));
    var index = layer.open({
        type: 2,
        title: title,
        content: url
    });
    layer.full(index);
}
function configuration(title,url,id) {
    sessionStorage.setItem("userid",id);
    var index = layer.open({
        type: 2,
        title: title,
        content: url
    });
    layer.full(index);
}

web.user_list=(function(){
    return function(page,current,obj,scrollLoad){
        var user_data = {}
        var succCallBack = function (data, status, response) {
            accountList(data);
        };
        return web.doAjax(root.accessInter.access_user_listUsersApi,'post',user_data, succCallBack);
    }
})();
/*停用*/
function user_stop(obj,id){
    layer.confirm('确认要停用吗？',function(index){
        console.log(id)
        $.ajax({
            type: "POST",
            url: root.accessInter.access_user_editS,
            cache: false,  //禁用缓存
            dataType: "json",
            data: {
                USER_ID: id,
                STATUS: "1"
            },  //传入组装的参数
            success: function (data) {
                switch (data.code) {
                    case 200:
                        layer.msg(data.data.errors[0], {icon: 1, time: 1000});
                        window.location.reload();
                    case 403:
                        layer.msg(data.data.errors[0], {icon: 1, time: 1000});

                }
            },
        });

    });
}

/*启用*/
function user_start(obj,id){
    layer.confirm('确认要启用吗？',function(index){
        $.ajax({
            type: "POST",
            url: root.accessInter.access_user_editS,
            cache: false,  //禁用缓存
            dataType: "json",
            data: {
                USER_ID: id,
                STATUS: "0"
            },  //传入组装的参数
            success: function (data) {
                switch (data.code) {
                    case 200:
                        layer.msg(data.data.errors[0], {icon: 1, time: 1000});
                        window.location.reload();
                    case 403:
                        layer.msg(data.data.errors[0], {icon: 1, time: 1000});

                }
            },
        });
        layer.msg('已启用!', {icon: 6,time:1000});
    });
}